<template>
    <div class="e-form" v-if="state === 'main'">
        <!-- <div class="title">
                <img src="@/assets/test.png" alt="" />
                <span>外包商入库</span>
                <i class="el-icon-full-screen" @click="handleFullScreen"></i>
            </div> -->
        <div class="tabs">
            <div class="tabs-title">基本信息</div>
            <div style="width: 100%" class="form">
                <el-form
                    :model="formData.baseInfo"
                    label-width="170px"
                    :rules="rules"
                    ref="ruleForm"
                >
                    <el-row>
                        <el-col :span="12">
                            <el-form-item label="招标编号：" prop="tenderApplyNo">
                                <div style="display: flex">
                                    <el-input
                                        v-model="formData.baseInfo.tenderApplyNo"
                                        disabled
                                    ></el-input>
                                    <ComShowBiddingButton :tenderId="tenderId" />
                                </div>
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item label="拟招标项目名称：" prop="projectName">
                                <span>{{ formData.baseInfo.projectName }}</span>
                            </el-form-item>
                        </el-col>
                    </el-row>
                    <el-row>
                        <el-col :span="12">
                            <el-form-item label="招标方式：" prop="tenderForm">
                                <span v-if="formData.baseInfo.tenderForm">{{
                                    formData.baseInfo.tenderForm | tenderForm(this)
                                }}</span>
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item label="招标类型：" prop="tenderClass">
                                <span>{{
                                    (formData.baseInfo.tenderType) | tenderType(this)
                                }}</span>
                            </el-form-item>
                        </el-col>
                    </el-row>
                    <el-row>
                        <el-col :span="12">
                            <el-form-item label="登记日期：" prop="reportTime">
                                <el-date-picker
                                    v-model="formData.baseInfo.reportTime"
                                    type="date"
                                    placeholder="选择日期"
                                    :clearable="false"
                                    value-format="yyyy-MM-dd"
                                >
                                </el-date-picker>
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item label="状态：" prop="state">
                                <span>{{ formData.baseInfo.state | state(this) }}</span>
                            </el-form-item>
                        </el-col>
                    </el-row>
                    <el-row>
                        <el-form-item label="评标记录：" prop="evaluationRecord">
                            <el-input
                                type="textarea"
                                :rows="2"
                                placeholder="请输入内容"
                                v-model="formData.baseInfo.evaluationRecord"
                                style="margin-right: 110px"
                            >
                            </el-input>
                        </el-form-item>
                    </el-row>
                    <el-row>
                        <el-col :span="12">
                            <el-form-item label="录入机构：" prop="orgName">
                                <span>{{ formData.baseInfo.orgName }}</span>
                            </el-form-item>
                        </el-col>
                    </el-row>
                </el-form>
            </div>
        </div>
        <div class="buttons">
            <el-button type="primary" class="btn-blue" @click="submit" size="small">保存</el-button>
            <el-button @click="handleClose" size="small">取消</el-button>
        </div>
    </div>
</template>

<script>
// import ComTreeTable from '@/templates/table/treeTable.vue'
// import ComTreeFile from '@/templates/table/treeFile.vue'
// import ComPagination from '@/components/pagination/pagination.vue'
import resultRequest from '@/api/tender/result.js'
import applyRequest from '@/api/tender/apply.js'
import ComShowBiddingButton from '@/pages/bidding/common/showBiddingButton.vue'
import { openAppClient } from '@/utils/common'
import { mapState } from 'vuex'

export default {
    data () {
        return {
            state: 'main',
            // dialogVisible: false,
            // fullscreen: false,
            form: {
                name: '',
                region: '',
                date1: '',
                date2: '',
                delivery: false,
                type: [],
                resource: '',
                desc: ''
            },
            tableData: [],
            value: '',
            textarea: '',
            formData: {
                baseInfo: {
                    billId: '',
                    billNo: '',
                    evaluationRecord: '',
                    gmtCreate: '',
                    orgId: '',
                    orgName: '',
                    projectName: '',
                    recorder: '',
                    recorderId: '',
                    remark: '',
                    reportTime: '',
                    state: 0,
                    tenderApplyId: '',
                    tenderApplyNo: '',
                    tenderApplyOrgId: '',
                    tenderApplyOrgName: '',
                    tenderClass: 0,
                    tenderType: 0,
                    tenderForm: '',
                    tenderFormName: '公开招标',
                    tenderOrgId: '',
                    tenderOrgName: '',
                    tenderUser: '',
                    tenderUserId: ''
                },
                candidateList: [
                    {
                        billId: '',
                        firstAmount: 0,
                        firstCandidate: '',
                        firstCandidateId: '',
                        firstGrade: 0,
                        gmtCreate: '',
                        gmtModified: '',
                        isFail: 0,
                        packageId: '',
                        packageName: '',
                        packageNo: '',
                        projectOverview: '',
                        recordId: '',
                        startEndStake: '',
                        subordinateUnit: '',
                        subordinateUnitName: '',
                        threeAmount: 0,
                        threeCandidate: '',
                        threeCandidateId: '',
                        threeGrade: 0,
                        twoAmount: 0,
                        twoCandidate: '',
                        twoCandidateId: '',
                        twoGrade: 0
                    }
                ]
            },
            test: '',
            selectData: [],
            selectTableColumnAttr: [
                {
                    label: '单据编号',
                    prop: 'billNo'
                },
                {
                    label: '拟招标项目名称',
                    prop: 'tenderName'
                },
                {
                    label: '单据日期',
                    prop: 'gmtCreate'
                },
                {
                    label: '单据机构名称',
                    prop: 'orgName'
                }
            ],
            selectTableColumnWidth: {
                leftWidth: '150',
                rightWidth: '100'
            },
            selectDataPagination: {},
            rules: {
                tenderApplyNo: {
                    required: true,
                    message: '请输入招标编号',
                    trigger: 'blur'
                },
                projectName: {
                    required: true,
                    message: '请输入拟招标项目名称',
                    trigger: 'blur'
                },
                tenderForm: {
                    required: true,
                    message: '请输入招标方式',
                    trigger: 'blur'
                },
                tenderClass: {
                    required: true,
                    message: '请输入招标类型',
                    trigger: 'blur'
                },
                reportTime: {
                    required: true,
                    message: '请输入报告日期',
                    trigger: 'blur'
                },
                state: {
                    required: true,
                    message: '请输入状态',
                    trigger: 'blur'
                },
                evaluationRecord: {
                    required: true,
                    message: '请输入评标记录',
                    trigger: 'blur'
                },
                orgName: {
                    required: true,
                    message: '请输入录入机构',
                    trigger: 'blur'
                }
            }
        }
    },
    computed: {
        ...mapState({
            options: state => state.biddingApplication.dictionaries
        }),
        tenderId () {
            return this.$route.query.tenderId || this.$route.query.billid
        }
    },
    components: {
        // ComTreeTable,
        // ComTreeFile,
        // ComPagination,
        ComShowBiddingButton
    },
    created () {
        ;(async () => {
            await this.tenderApplyFindById()
            this.getNowDate()
        })()
    },
    mounted () {},
    methods: {
        getNowDate () {
            // 个位数前补0
            const zeroFill = function (num) {
                if (parseInt(num) < 10) {
                    num = '0' + num
                }
                return num
            }
            let nowtime = new Date()
            nowtime =
                zeroFill(nowtime.getFullYear()) +
                '-' +
                zeroFill(nowtime.getMonth() + 1) +
                '-' +
                zeroFill(nowtime.getDate())
            this.$set(this.formData.baseInfo, 'reportTime', nowtime)
        },
        selectChange (kvId, label) {
            if (label === '采购方式') {
                this.options.tenderForm.forEach(item => {
                    if (kvId === item.value) {
                        this.formData.tenderFormName = item.label
                    }
                })
            } else if (label === '币种') {
                this.options.currency.forEach(item => {
                    if (item.kvValue === kvId) {
                        this.formData.currency = item.kvKey
                    }
                })
            }
        },
        async tenderApplyFindById () {
            await applyRequest.tenderApplyFindById(this.tenderId).then(res => {
                // 表单提交信息映射基础招标信息
                const tenderData = res
                Object.keys(this.formData.baseInfo).forEach(formDataKey => {
                    Object.keys(tenderData).forEach(tenderDataKey => {
                        if (formDataKey === tenderDataKey) {
                            this.formData.baseInfo[formDataKey] = tenderData[tenderDataKey]
                        }
                    })
                })
                // 部分数据对应不上，需要增改
                this.formData.baseInfo.tenderApplyId = tenderData.billId // res的billid是招标id，新增补遗招标id是tenderApplyId
                this.formData.baseInfo.tenderType = tenderData.tenderType
                this.formData.baseInfo.tenderApplyNo = tenderData.billNo
                delete this.formData.baseInfo.billId // 新增不需要表单的billId
                delete this.formData.baseInfo.billNo
                delete this.formData.baseInfo.workId // 新增不需要表单的workId
                this.formData.baseInfo.projectName = res.tenderName
                this.formData.baseInfo.state = 0 // 状态默认为草稿
            })
        },
        showBidding () {
            // 客户端弹窗弹出本地页面
            openAppClient(`/biddingApplicationShow?see=true&tenderId=${this.tenderId}`, '查看招标')
        },
        selectCurrentChange (page) {
            this.tenderApplyQueryPage({ limit: 20, page })
        },
        selectDataSearch (searchValue) {
            this.tenderApplyQueryPage({ key: searchValue, limit: 20, page: 1 })
        },
        selectDataConfirm (data) {
            this.state = 'main'
            this.formData.applyOrgName = data.orgName
            this.formData.applyOrgId = data.orgId
        },
        tenderApplyNoInputFocus () {
            this.$refs.tenderApplyNoInput.blur()
        },
        stateChange (name) {
            this.state = name
            if (name === 'selectData') {
                this.tenderApplyQueryPage()
            }
        },
        tenderApplyQueryPage (params = { limit: 20, page: 1 }) {
            applyRequest.tenderApplyQueryPage(params).then(res => {
                this.selectData = res.list
                this.selectDataPagination.total = res.totalCount
            })
        },
        // dialogCloseHook () {
        //     this.initData()
        // },
        // initData () {
        //     this.formData = {
        //         state: 0
        //     }
        // },
        submit () {
            this.$refs.ruleForm.validate(async valid => {
                if (valid) {
                    resultRequest.save(this.formData).then(res => {
                        const callBack = () => {
                            this.$router.push({
                                path: './bidEvaluationResultEdit',
                                query: { billid: res }
                            })
                        }
                        const cancelCallBack = () => {
                            this.$router.push({
                                path: './biddingApplicationEdit',
                                query: { billid: this.tenderId, seat: 'bidEvaluation' }
                            })
                        }
                        this.clientPop(
                            'suc',
                            '保存成功，是否继续？',
                            callBack,
                            null,
                            cancelCallBack
                        )
                    })
                } else {
                    return false
                }
            })
        },
        // getVuexData () {
        //     // 获取保存在vuex的数据
        //     this.formData = {
        //         ...this.formData,
        //         ...JSON.parse(
        //             JSON.stringify(
        //                 this.$store.state.biddingApplication.formData
        //             )
        //         )
        //     }
        //     // 获取保存在vuex的常量
        //     if (Object.keys(this.$store.state.userInfo.orgInfo).length !== 0) {
        //         this.formData.orgId = this.$store.state.userInfo.orgInfo.orgId
        //         this.formData.orgName =
        //             this.$store.state.userInfo.orgInfo.orgName
        //     }
        //     console.log('this.formData1', this.formData)
        // },
        handleFullScreen () {
            this.fullscreen = !this.fullscreen
        },
        handleClose () {
            this.$router.push({
                path: './biddingApplicationEdit',
                query: { billid: this.tenderId, seat: 'bidEvaluation' }
            })
        },
        cellClsNm ({ column }) {
            if (column.label === '') {
                return 'fixed-left'
            }
        },
        cellMouseEnter (row) {
            this.MouseEnterId = row.id
        },
        cellMouseLeave () {
            this.MouseEnterId = 0
        }
    }
}
</script>

<style lang="scss" scoped>
.buttons {
    left: 0;
}
.e-form {
    padding: 0 20px;
    .tabs-title::before {
        content: '';
        height: 22px;
        width: 8px;
        border-radius: 40px;
        background-color: #2e61d7;
        display: block;
        position: absolute;
        left: 20px;
        margin-right: 20px;
    }
}
.form {
    margin-top: 10px;
}

.e-table {
    background: #fff;

    /* .top {
    .left {
      padding: 20px;
      padding-top: 0px;
    }
  } */
}

.upload {
    margin: 20px auto;
    display: flex;
    justify-content: center;
    text-align: center;
}

.upload-demo {
    display: flex;
    justify-content: center;
    align-items: center;
}
</style>
